package com.link.base.core.basic.util.datax;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务中台
 *
 * @author liying
 * @date 2019/06/03 09:56
 */
public class YeWuZhongTai {

    /**
     * 业务中台：订单头数据同步
     *
     * @author liying
     * @date 2019/06/03 08:03
     */
    public static String orderTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "trade_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_orders";
        // 正式表名称
        String targetTableName = "lnk_sale_order";

        List<FieldMapping> fieldList = new ArrayList<>();

        fieldList.add(new FieldMapping("'YeWuZhongTai'", "source_from", "VARCHAR(100)"));
        fieldList.add(new FieldMapping(
                "(select s1.DMP_ID from linkcrm.jxb_mapping s1 where s1.MAPPING_TYPE = 'ShopPlatform' and s1.MAPPING_ID = t1.shop_no limit 1)",
                "source", "varchar(100)").setRequired(true));
        fieldList.add(new FieldMapping("shop_no", "shop_code", "varchar(100)"));
        fieldList.add(new FieldMapping("trade_no", "order_no", "varchar(100)"));
        fieldList.add(new FieldMapping("src_tids", "original_order_no", "varchar(100)"));
        fieldList.add(new FieldMapping("(str_to_date(t1.trade_time, '%Y-%m-%d %H:%i:%S'))", "order_date", "datetime"));
        fieldList.add(new FieldMapping("'SalesOrder'", "order_type", "varchar(100)"));
        fieldList.add(new FieldMapping("trade_status", "status", "varchar(100)"));
        fieldList.add(new FieldMapping("receivable", "order_total_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("goods_amount", "ord_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("post_amount", "freight", "decimal(22,7)"));
        fieldList.add(new FieldMapping("other_amount", "other_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("discount", "ord_discount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("receiver_name", "consignee", "varchar(100)"));
        fieldList.add(new FieldMapping("receiver_phone", "consignee_phone", "varchar(50)"));
        fieldList.add(new FieldMapping("receiver_province", "province", "varchar(100)"));
        fieldList.add(new FieldMapping("receiver_city", "city", "varchar(100)"));
        fieldList.add(new FieldMapping("receiver_district", "district", "varchar(100)"));
        fieldList.add(new FieldMapping("receiver_address", "address", "varchar(254)"));
        fieldList.add(new FieldMapping("openid", "wx_open_id", "varchar(100)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "source_from");
    }

    /**
     * 业务中台：订单行数据同步
     *
     * @author liying
     * @date 2019/06/03 09:09
     */
    public static String orderItemTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "rec_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_order_goods";
        // 正式表名称
        String targetTableName = "lnk_sale_order_item";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "order_source", "varchar(50)"));
        fieldList.add(new FieldMapping("'SalesOrder'", "order_item_type", "varchar(100)"));
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_sale_order s1 where "
                + " s1.source_from = 'YeWuZhongTai' and s1.integration_id = t1.trade_id limit 1)",
                "head_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_prod s1 "
                + "where s1.prod_good_type = 'YeWuZhongTai' and s1.prod_code = t1.goods_no limit 1)",
                "prod_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("num", "prod_num", "decimal(22,7)"));
        fieldList.add(new FieldMapping("price", "base_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("discount", "promo_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("order_price", "net_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("share_amount", "price", "decimal(22,7)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }

    /**
     * 业务中台：退换货订单同步
     *
     * @author liying
     * @date 2019/06/03 09:26
     */
    public static String returnOrderTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "refund_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_refund_orders";
        // 正式表名称
        String targetTableName = "lnk_sale_order";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "source_from", "VARCHAR(100)"));
        fieldList.add(new FieldMapping(
                "(select s1.DMP_ID from linkcrm.jxb_mapping s1 where s1.MAPPING_TYPE = 'ShopPlatform' and s1.MAPPING_ID = t1.shop_no limit 1)",
                "source", "varchar(100)").setRequired(true));
        fieldList.add(new FieldMapping("shop_no", "shop_code", "varchar(100)"));
        fieldList.add(new FieldMapping("refund_no", "order_no", "varchar(100)"));
        fieldList.add(new FieldMapping("tid", "original_order_no", "varchar(100)"));
        fieldList.add(new FieldMapping("sales_tid", "assoc_ord_id", "varchar(100)"));
        fieldList.add(new FieldMapping("swap_trade_no", "original_order_no", "varchar(100)"));
        fieldList.add(new FieldMapping("(str_to_date(t1.refund_time, '%Y-%m-%d %H:%i:%S'))", "order_date", "datetime"));
        fieldList.add(new FieldMapping("type", "order_child_type", "varchar(100)"));
        fieldList.add(new FieldMapping("'ReturnOrder'", "order_type", "varchar(100)"));
        fieldList.add(new FieldMapping("process_status", "status", "varchar(100)"));
        fieldList.add(new FieldMapping("actual_refund_amount", "order_total_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("goods_amount", "ord_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("post_amount", "freight", "decimal(22,7)"));
        fieldList.add(new FieldMapping("other_amount", "other_amount", "decimal(22,7)"));
        fieldList.add(new FieldMapping("openid", "wx_open_id", "varchar(100)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "source_from");
    }

    /**
     * 业务中台：退货商品同步
     *
     * @author liying
     * @date 2019/06/03 09:37
     */
    public static String returnProdTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "order_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_refund_orders_info";
        // 正式表名称
        String targetTableName = "lnk_sale_order_item";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "order_source", "varchar(50)"));
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_sale_order s1 where "
                + " s1.source_from = 'YeWuZhongTai' and s1.integration_id = t1.refund_id limit 1)",
                "head_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_prod s1 "
                + "where s1.prod_good_type = 'YeWuZhongTai' and s1.prod_code = t1.goods_no limit 1)",
                "prod_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("refund_num", "prod_num", "decimal(22,7)"));
        fieldList.add(new FieldMapping("total_amount", "base_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("discount", "promo_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("price", "net_pri", "decimal(22,7)"));
        fieldList.add(new FieldMapping("total_amount", "price", "decimal(22,7)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }

    /**
     * 业务中台：换货商品同步
     *
     * @author liying
     * @date 2019/06/03 09:42
     */
    public static String exchangeProdTemp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "rec_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_refund_goods_list";
        // 正式表名称
        String targetTableName = "lnk_sale_order_item";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "order_source", "varchar(50)"));
        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_sale_order s1 where "
                + " s1.source_from = 'YeWuZhongTai' and s1.integration_id = t1.refund_id limit 1)",
                "head_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("(select s1.row_id from linkcrm.lnk_prod s1 "
                + "where s1.prod_good_type = 'YeWuZhongTai' and s1.prod_code = t1.merchant_no limit 1)",
                "prod_id", "varchar(50)").setRequired(true));

        fieldList.add(new FieldMapping("num", "prod_num", "decimal(22,7)"));
        fieldList.add(new FieldMapping("retail_price", "base_pri", "decimal(22,7)"));

        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }
    
    /**
     * @Description //将业务中台的jxb_transfer_product表同步到LNK_PROD表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbProdTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "product_code";
        // 临时表名称
        String tempTableName = "ywzt_transfer_product";
        // 正式表名称
        String targetTableName = "LNK_PROD";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));
        fieldList.add(new FieldMapping("PROD_CODE", "product_code", "bigint(13)"));
        fieldList.add(new FieldMapping("PROD_NAME", "product_name", "varchar(100)"));
        fieldList.add(new FieldMapping("SHORT_NAME", "short_name", "varchar(50)"));
        fieldList.add(new FieldMapping("OLD_PROD_CODE", "old_code", "varchar(50)"));
        fieldList.add(new FieldMapping("OLD_PROD_NAME", "old_name", "varchar(255)"));
        fieldList.add(new FieldMapping("LEGAL_ENTITY_CODE", "outside_code", "int(8)"));
        fieldList.add(new FieldMapping("BRAND_CODE", "brand_code", "varchar(20)"));
        fieldList.add(new FieldMapping("CATEGORY_CODE", "cate_code", "varchar(4)"));
        fieldList.add(new FieldMapping("ML_NAME", "mm_pool", "varchar(100)"));
        fieldList.add(new FieldMapping("SPIRIT_NAME", "spirit_name", "varchar(10)"));
        fieldList.add(new FieldMapping("PROD_STANDARD", "product_spec", "varchar(50)"));
        fieldList.add(new FieldMapping("PROD_UNIT", "minunit_code", "varchar(3)"));
        fieldList.add(new FieldMapping("IS_EFFECTIVE", "produt_status", "int(4)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }

    /**
     * @Description //将业务中台的jxb_transfer_wide_product表同步到LNK_PROD表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbWideProdTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "wide_code";
        // 临时表名称
        String tempTableName = "ywzt_transfer_wide_product";
        // 正式表名称
        String targetTableName = "LNK_PROD";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));
        fieldList.add(new FieldMapping("PROD_CODE", "wide_code", "bigint(9)"));
        fieldList.add(new FieldMapping("PROD_NAME", "wide_name", "varchar(100)"));
        fieldList.add(new FieldMapping("PROD_STANDARD", "wide_spec", "varchar(100)"));
        fieldList.add(new FieldMapping("SHORT_NAME", "short_name", "varchar(50)"));
        fieldList.add(new FieldMapping("LEGAL_ENTITY_CODE", "outside_code", "bigint(14)"));
        fieldList.add(new FieldMapping("OLD_PROD_CODE", "old_code", "varchar(25)"));
        fieldList.add(new FieldMapping("OLD_PROD_NAME", "old_name", "varchar(100)"));
        fieldList.add(new FieldMapping("PROD_UNIT", "minunit_code", "varchar(3)"));
        fieldList.add(new FieldMapping("IS_EFFECTIVE", "wide_status", "int(4)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }


    /**
     * @Description //将业务中台的jxb_transfer_legal_entity表同步到JXB_LEGAL_ENTITY表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbLegalEntityTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "outside_code";
        // 临时表名称
        String tempTableName = "ywzt_transfer_legal_entity";
        // 正式表名称
        String targetTableName = "JXB_LEGAL_ENTITY";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));

        fieldList.add(new FieldMapping("LEGAL_ENTITY_CODE", "outside_code", "int(2)"));
        fieldList.add(new FieldMapping("LEGAL_ENTITY_NAME", "outside_name", "varchar(25)"));
        fieldList.add(new FieldMapping("LEGAL_ENTITY_ABBR", "short_name", "varchar(25)"));
        fieldList.add(new FieldMapping("is_effective", "outside_status", "int(4)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }


    /**
     * @Description //将业务中台的jxb_transfer_brand表同步到JXB_PROD_BRAND表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbBrandTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "brand_code";
        // 临时表名称
        String tempTableName = "ywzt_transfer_brand";
        // 正式表名称
        String targetTableName = "JXB_PROD_BRAND";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));

        fieldList.add(new FieldMapping("BRAND_CODE", "brand_code", "varchar(4)"));
        fieldList.add(new FieldMapping("BRAND_NAME", "brand_name", "varchar(20)"));
        fieldList.add(new FieldMapping("IS_EFFECTIVE", "brand_status", "int(4)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }

    /**
     * @Description //将业务中台的jxb_transfer_brand_category表同步到JXB_PROD_CATEGORY表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbBrandCategoryTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "cate_code";
        // 临时表名称
        String tempTableName = "ywzt_transfer_brand_category";
        // 正式表名称
        String targetTableName = "JXB_PROD_CATEGORY";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));

        fieldList.add(new FieldMapping("CATEGORY_CODE", "cate_code", "varchar(4)"));
        fieldList.add(new FieldMapping("CATEGORY_NAME", "cate_name", "varchar(20)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "order_source");
    }


    /**
     * @Description //将业务中台的jxb_transfer_area表同步到JXB_ORDER_ADDR表
     * @Author zhiqiang.jiang
     * @Date    2019/6/29-15:04
     */
    public static String returnJxbOrderAddrTmp() {
        //临时表主键名称
        String tempTablePrimaryKeyFieldName = "area_id";
        // 临时表名称
        String tempTableName = "ywzt_transfer_area";
        // 正式表名称
        String targetTableName = "JXB_ORDER_ADDR";

        List<FieldMapping> fieldList = new ArrayList<>();
        fieldList.add(new FieldMapping("'YeWuZhongTai'", "SOURCE", "varchar(50)"));

        fieldList.add(new FieldMapping("area_id", "AREA_ID", "bigint(20)"));
        fieldList.add(new FieldMapping("area_name", "AREA_NAME", "varchar(255)"));
        return DataxUtil.createProcedureCode(tempTablePrimaryKeyFieldName, tempTableName, targetTableName, fieldList, "from_platform");
    }
}
